package com.ceneti.usda.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ceneti.usda.exception.ApplicationException;
import com.ceneti.usda.service.UsdaService;
import com.ceneti.usda.vo.GroupComboVo;
import com.ceneti.usda.vo.LabelValueVo;
import com.ceneti.usda.vo.NutrientGridVo;

/**
 * 
 * @author leopoldo
 *
 */
@Controller
@RequestMapping("/main")
public class UsdaController {
	
	
	@Autowired
	@Qualifier("usdaService")
	private UsdaService usdaService;
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping( value="exception.action", method= RequestMethod.GET )
	public @ResponseBody Map<String, ? extends Object> sendException( HttpServletRequest request, HttpServletResponse response ){
		
		Map<String, Object> requestMap = new HashMap<String, Object>();
		boolean isSuccess = false;
		String message = null;
		
		try {
			throw new ApplicationException();
		} catch (ApplicationException e) {
			e.printStackTrace();
			
			System.out.println("sending error message");
			
			message= "exception on application";
			response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			
		}
		
		requestMap.put("message", message);
		requestMap.put("success", isSuccess);
		
		return requestMap;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="GoUsdaJQuery.action", method= RequestMethod.GET )
	public String goToUsda(){
		
		return "/html/UsdaSearch.html";
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="GoNutrientsJQuery.action", method= RequestMethod.GET )
	public String goToNutrients(){
		
		return "/html/UsdaDetail.html";
	}
	

	/**
	 * 
	 * @param foodId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="checkFoodId.action", method= RequestMethod.GET )
	public @ResponseBody  Map<String, ? extends Object> checkFoodAvailable( @RequestParam(value="foodId")  String foodId, 
			HttpServletRequest request, HttpServletResponse response   ){
		
		Map<String, Object> requestMap = new HashMap<String, Object>();
		boolean isSuccess = false;
		String message = null;
		
		boolean isFoodAvailable = false;
		
		try {
			isFoodAvailable = this.usdaService.checkFoodId(foodId);
			message = "";
			isSuccess = true;
		} catch (ApplicationException e) {
			e.printStackTrace();
			message= "exception on application";
			response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		
		requestMap.put( "isFoodAvailable", isFoodAvailable );
		
		requestMap.put("message", message);
		requestMap.put("success", isSuccess);
		
		return requestMap;
	}
	
	
	/**
	 * 
	 * @param foodDescription
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="loadGroups.action", method= RequestMethod.GET )
	public @ResponseBody  Map<String, ? extends Object> loadFoodGroups( @RequestParam(value="description")  String foodDescription, 
			HttpServletRequest request, HttpServletResponse response ){
		
		Map<String, Object> requestMap = new HashMap<String, Object>();
		boolean isSuccess = false;
		String message = null;
		
		try {
			List<GroupComboVo> itemList =  this.usdaService.loadGroupsItems(foodDescription);
			requestMap.put("items", itemList);
			message = "";
			isSuccess = true;
		} catch (ApplicationException e) {
			e.printStackTrace();
			message= "exception on application";
			response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		
		requestMap.put("message", message);
		requestMap.put("success", isSuccess);
		
		return requestMap;
	}
	
	/**
	 * 
	 * @param foodDescription
	 * @param groupId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping( value="loadFoods.action", method= RequestMethod.GET )
	public @ResponseBody  Map<String, ? extends Object> loadFoodItems(  @RequestParam(value="description") String  foodDescription, @RequestParam(value="groupId") String groupId, 
			HttpServletRequest request, HttpServletResponse response ){
		
		Map<String, Object> requestMap = new HashMap<String, Object>();
		boolean isSuccess = false;
		String message = null;
		
		try {
			List<LabelValueVo> itemList = this.usdaService.loadFoodItems(foodDescription, groupId);
			requestMap.put("items", itemList);
			message = "";
			isSuccess = true;
		} catch (ApplicationException e) {
			e.printStackTrace();
			message= "exception on application";
			response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		
		requestMap.put("message", message);
		requestMap.put("success", isSuccess);
		
		return requestMap;
	}
	
	/**
	 * 
	 * @param foodId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping (value="loadNutrients.action", method= RequestMethod.GET )
	public Map<String, ? extends Object> loadFoodNutrients(  @RequestParam(value="foodId", required = true) String foodId, 
			HttpServletRequest request, HttpServletResponse response ){
		
		Map<String, Object> requestMap = new HashMap<String, Object>();
		boolean isSuccess = false;
		String message = null;
		
		try {
		    NutrientGridVo nutrientGrid = this.usdaService.loadNutrientItems(foodId);
			requestMap.put("nutrientsGrid", nutrientGrid );
		    message = "";
			isSuccess = true;
		} catch( ApplicationException e) {
			e.printStackTrace();			
			message= "exception on application";
			response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		
		requestMap.put("message", message);
		requestMap.put("success", isSuccess);
		
		return requestMap;
	}
	
	
	
}
